/**
 * Created by weng on 2017/8/20.
 */
var db = require('../common/dataAccess');
var  moment = require("moment");
/**
 * 获取在院老人信息
 * @param parame
 * @param req
 * @param cb
 */
var getAccountList = function (parame,req,cb) {
    var phone = parame.phone;

    var obj = {};
    obj.commands = "select * from taccount;";
    if(phone)
        obj.commands = "select * from taccount where phone = '" + phone + "';";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"修改失败"});
        else
            cb({code:200,info:info});
    })
};


/**
 * 修改电话号码
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var updatePhone = function (parame,req,cb) {
    console.log("updatePhone",parame);
    if(! parame.phone || ! parame.id){
        return cb({code:500,info:"修改失败"});
    }
    var obj = {};
    obj.commands = "update taccount  set phone = '"  + parame.phone + "' where id = " + parame.id;
    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"修改失败"});
        else
            return cb({code:200,info:"修改成功"});
    })
};

/**
 * 新建帐号
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var saveAccount = function (parame,req,cb) {
    console.log("saveAccount",parame);
    var id = parame.id,
        name = parame.name,
        sex = parame.sex,
        phone = parame.phone,
        birthday =parame.birthday;

    if(!id || !name || !sex || !phone){
        return cb({code:500,info:"缺少必要参数"});
    };


    var obj = {};
    obj.commands = "select count(*) total from taccount where id = " + id + ";";
    obj.commands += "select count(*) total from taccount where phone = '" + phone + "'";

	
    db.runSqlInfo(obj,function(infoAccount,err){
        console.log("info is:",infoAccount);
        if(err)
            return cb({code:500,info:"修改失败"});
        else{
			 console.log("info is:",infoAccount[0][0].total);
			 console.log("info is:",infoAccount[1][0].total);
			 
            if(infoAccount[0][0].total > 0){
                return cb({code:500,info:"住院号已存在请重新输入"});
            }else if(infoAccount[1][0].total > 0){
              return cb({code:500,info:"电话号码重复"});
            }
            
            var obj = {};
            obj.commands = "insert into taccount(id,hospitalId,name,sex,phone,birthday) values ("
                + "'" + id + "'"
                + ",'" + id + "'"
                + ",'" + name + "'"
                + ",'" + sex + "'"
                + ",'" + phone + "'"
                + ",'" + birthday + "');insert into taccountex(id) values("
                + "'" + id + "');"
            ;
            db.runSqlInfo(obj,function(info,err){
                console.log("info is:",info);
                if(err)
                    return cb({code:500,info:"修改失败"});
                else
                    return cb({code:200,info:"修改成功"});
            })
        }

    })
}

/**
 * 获取预约车信息
 * @param parame
 * @param req
 * @param cb
 */
var getBusOrder = function (parame,req,cb) {
    var time = parame.time
    if(!time)
        time = moment().format('YYYY-MM-DD');

    var obj = {};
    obj.commands =
    "select DISTINCT t1.id,t1.*,ifnull(t2.hospitalId,'无') as hospitalId ,ifnull(t2.name,'无') as name "
	 + "  from ordertime t1 "
	 + "  left join ( "
	 + "    SELECT t1.name,t1.hospitalId,t1.phone ,t2.phone as phone2 FROM taccount  t1 left JOIN trelation t2 on t1.id = t2.id "
	 + "  )t2 "
	 + "  on t1.phone = t2.phone or t1.phone = t2.phone2 "
	 + " where  t1.time >= '" + time + "'";
    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"查询失败"});
        else
            return cb({code:200,info:info});
    })
};


/**
 * 老人信息详情
 * @param req
 * @param res
 * @param cb
 */
var taccountInfo =function (req,res,cb) {
    var form = req.query;

    var id = form.id;//.toLowerCase();

    var obj = {};
    obj.commands = "select t1.*,t2.* "
   
        + " from  taccount t1  left join taccountex t2 on t1.id = t2.id  where t1.id = '" + id + "';";
    obj.commands += "select * from  trelation  where id = '" + id + "';";

    db.runSqlInfo(obj, function (info, err) {
        console.log("info is",info)
        if (err) {
            res.redirect('/boss/index')
        }
        else {
            res.render('boss/taccountInfo', {
                data:info[0][0],
                data_relation:info[1]
            })
        }
    });
}




var AddTaccountDetail =function (req,res,cb) {
    var form = req.query;
    var id = form.id;//.toLowerCase();
    var flag = form.flag;//.toLowerCase();

    res.render('boss/AddTaccountDetail', {
        id:id,
        flag:flag
    })
}


/**
 * app帐号列表
 * @param req
 * @param res
 * @param cb
 */
var getTaccountAppList = function (parame,req,cb) {
    console.log("getTaccountAppList",parame);
    var phone = parame.phone
    var obj = {};

    obj.commands = "select * from taccount_app";
    if(phone)
        obj.commands = "select * from taccount_app where phone = '" + phone + "'";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"修改失败"});
        else
            return cb({code:200,info:info});
    })
};


/**
 * 修改密码
 * @param parame
 * @param req
 * @param cb
 */
var updateTaccountApp = function (parame,req,cb) {
    console.log("getTaccountAppList",parame);
    var phone = parame.phone
    var password = parame.password
    var obj = {};
    if(!phone)
        return cb({code:500,info:"缺少必要参数"});

    obj.commands = "update  taccount_app set password  = "
        + "'" + password + "'"
        + " where phone = '" + phone + "'";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"修改失败"});
        else
            return cb({code:200,info:info});
    })
}

/**
 * 获得预约班车设定
 * @param parame
 * @param req
 * @param cb
 */
var getOrderInfo = function(parame,req,cb) {
    var time = moment().format('YYYY-MM-DD');
    var obj = {};
    obj.commands = "select * from  bustime where time in (1,2,3,4,5,6,0) order by time;";
    obj.commands += "select * from  bustime where status = 1 and  time not in (1,2,3,4,5,6,0) and time >='" + time + "'";

    db.runSqlInfo(obj, function (info, err) {
        console.log("info is",info)
        if (err) {
            return cb({code:500,info:"数据库查询错误"})
        }
        else {
            return cb({code:200,date:info[0],spdate:info[1]})
        }
    });
}

/**
 * 修改预约时间
 * @param parame
 * @param req
 * @param cb
 */
var changeBusTime = function(parame,req,cb) {
    console.log("changeBusTime parame is",parame);

    var  id= parame.id;
    var  bustype = parame.bustype;
    var  address = parame.address;

    if(!id || !bustype ||!address){
        return cb({code:500,info:"参数错误"})
    }

    var obj = {};
    obj.commands = "update  bustime set "
        + " bustype = '" + bustype + "', "
        + " address = '" + address + "' "
        +"  where id = '" + id + "'" ;

    db.runSqlInfo(obj, function (info, err) {
        console.log("info is",info)
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:"修改成功"});
        }
    });
}

/**
 * 删除特殊班次
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var delBusTime = function(parame,req,cb) {
    console.log("delBusTiime",parame);

    var  id= parame.id;
    var  status= JSON.parse(parame.status);
    if(!id || isNaN(status)){
        return cb({code:500,info:"参数错误"})
    }
    var obj = {};
    obj.commands = "update bustime  set status = "+ !status +"   where id = '" + id + "'" ;

    db.runSqlInfo(obj, function (info, err) {
        console.log("info is",info)
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:"修改成功"});
        }
    });
}


/**
 * 添加预约时间
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var addBussTime = function(parame,req,cb) {
    console.log("addBussTime is",parame);

    var  time= parame.time;
    var  bustype= parame.bustype;
    var  address= parame.address;

    if(!time ||  !bustype || !address){
        return cb({code:500,info:"参数错误"})
    }
    var obj = {};
    obj.commands = "insert into bustime(time,address,bustype) values("
        + "'" + time + "',"
        + "'" + address + "',"
        + "'" + bustype + "');"

    db.runSqlInfo(obj, function (info, err) {
        console.log("info is",info)
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:"修改成功"});
        }
    });
}

/**
 * 获取推送信息
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var getPushInfo = function(parame,req,cb) {
    var  startTime= parame.startTime;
    var  endTime= parame.endTime;

    if(!startTime ||  !endTime){
        return cb({code:500,info:"参数错误"})
    }
    var obj = {};
    obj.commands = "select * from pushinfo where "
        + "time >= '" + startTime  + "'"
        + "and time <= '" + endTime  + "' order by time desc ;"

    db.runSqlInfo(obj, function (info, err) {
        console.log("info is",info)
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
}


/**
 * 极光推送
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var JPush = require('jpush-sdk')
var fs = require('fs')
const  path = require('path');
const  conf = require("../config/config.json")
var pushJG = function(parame,req,cb) {
    console.log("parame is",parame);
    var  title = parame.title;
    var  phone = parame.phone;
    var  url = "";
    var  info = parame.info;
    var  msg = parame.msg;
    var  type = parame.type;

    if(!title ||  !msg){
        return cb({code:500,info:"标题或者内容缺少"})
    }
    if(info && typeof info == 'string'){
        info = JSON.parse(info);
    }
    var setAudience;

    if(phone){
        setAudience = JPush.alias(phone)
        var obj = {};
        obj.commands = "select count(*) total from taccount_app where phone in ('" + phone  + "')";
        var phoneLength = phone.split(",").length;
        console.log("phone is ",phone);
        console.log("phoneLength is ",phoneLength);
        db.runSqlInfo(obj, function (infoCount, err) {
            console.log("info is",infoCount)
            if (err) {
                return cb({code:500,info:"数据库查询错误"});
            }
            else {
                if(info[0] && infoCount[0].total < phoneLength){
                    return cb({code:500,info:"电话号码不对"});
                }
                if(req.files.files){
					var tempTime =new Date().getTime();
                    var originalPath = req.files.files.path;
                    var targetPath = path.join(__dirname, '../public/image/pushImg/'+ tempTime  +".png");
                    fs.createReadStream(originalPath).pipe(fs.createWriteStream(targetPath));
                    console.log("url is",targetPath);
                    var httpUrl = conf.url;
                    var url = httpUrl + '/image/pushImg/'+tempTime +".png";
                }

                var client = JPush.buildClient('f8e24c45505df69b4e464382', '51557c8d9b7fb986319898f7')

                var sentInfo = {msg:msg,info:info}
                if(url){
                    sentInfo.img = url;
                    sentInfo.style = 3;
                }else{
                    sentInfo.style = 1;
                }
                console.log("sentInfo",sentInfo);

                client.push().setPlatform('ios', 'android')
                    .setAudience(setAudience)
                    //.setAudience(JPush.alias(phone))
                    .setNotification(title,  JPush.ios(title,null,null,null,sentInfo)
                        ,JPush.android(title , null, 1,sentInfo)
                    ) .setOptions(null, null,null,true)
                    .send(function (err, res) {
                        if (err) {
                            if (err instanceof JPush.APIConnectionError) {
                                console.log(err.message)
                                // Response Timeout means your request to the server may have already received,
                                // please check whether or not to push
                                console.log(err.isResponseTimeout)
                            } else if (err instanceof JPush.APIRequestError) {
                                console.log(err.message)
                            }
                        } else {
                            console.log('Sendno: ' + JSON.stringify(res))
                            console.log('Msg_id: ' + res.msg_id)
                            var id = res.msg_id;

                            var obj = {};
                            obj.commands = "insert into  pushinfo (id,title,msg,phone,url) values( "
                                + " '" + id + "',"
                                + " '" + title + "',"
                                + " '" + msg + "',"
                                + " '" + phone + "',"
                                + " '" + url + "');"
                            ;
                            for(var i in info){
                                obj.commands += "insert into pushdetail values("
                                    + " '" + id + "',"
                                    + " '" + info[i].key + "',"
                                    + " '" + info[i].value + "');"
                            }
                            db.runSqlInfo(obj, function (info, err) {
                                console.log("info is",info)
                                if (err) {
                                    return cb({code:500,info:"数据库查询错误"});
                                }
                                else {
                                    return cb({code:200,info:info});
                                }
                            });
                        }
                    })
            }
        });

    }else {
        return cb({code:500,info:"电话号码不正确"});
        // setAudience = JPush.ALL;
    }

}


/**
 * 修改老人信息
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var updateTaccount = function(parame,req,cb) {
    console.log("parame",parame)

    var parame = JSON.parse(parame.obj);
    var  id= parame.id;
    if(req.files.files){
        var originalPath = req.files.files.path;
        var targetPath = path.join(__dirname, '../public/image/Taccount/'+ id  +".png");
        fs.createReadStream(originalPath).pipe(fs.createWriteStream(targetPath));
        //var httpUrl = conf.url;
        var url = '/image/Taccount/'+ id +".png";
    }

    var hospitalId = parame.hospitalId;
    var roomId = parame.roomId;
    var sex = parame.sex;
    var phone = parame.phone;
    var birthday = parame.birthday;
    var IdCard = parame.IdCard;
    var history = parame.history;
    var allergy = parame.allergy;
    var level = parame.level;
    var total = parame.total;
    var payInfo = parame.payInfo;
    var vacate = parame.vacate;
    var call = parame.call;
    var risk = parame.risk;
    var food = parame.food;
    var feed = parame.feed;
    var festival = parame.festival;
    var extraMeal = parame.extraMeal;
    var WEpay = parame.WEpay;
    var electric = parame.electric;
    var bed = parame.bed;
    var consumable = parame.consumable;
    var standbyMoney = parame.standbyMoney;
    var address = parame.address;
    var unit = parame.unit;
    var intoRoomTime = parame.intoRoomTime;
    var outRoomTime = parame.outRoomTime;
    var extendInfo = parame.extendInfo;
    var name1 = parame.name1;
    var name2 = parame.name2;
    var name3 = parame.name3;
    var relation1 = parame.relation1;
    var relation2 = parame.relation2;
    var relation3 = parame.relation3;
    var phone1 = parame.phone1;
    var phone2 = parame.phone2;
    var phone3 = parame.phone3;
    var uid3 = parame.uid3;
    var uid2 = parame.uid2;
    var uid1 = parame.uid1;

    var wechat1 = parame.wechat1;
    var wechat2 = parame.wechat2;
    var wechat3 = parame.wechat3;

    var reuint1 = parame.reuint1;
    var reuint2 = parame.reuint2;
    var reuint3 = parame.reuint3;



    var names = [];
    names.push(name1);
    names.push(name2);
    names.push(name3);
    var uids = [];
    uids.push(uid1);
    uids.push(uid2);
    uids.push(uid3);

    var relations = [];
    relations.push(relation1);
    relations.push(relation2);
    relations.push(relation3);

    var phones = [];
    phones.push(phone1);
    phones.push(phone2);
    phones.push(phone3);

    var wechat = [];
    wechat.push(wechat1);
    wechat.push(wechat2);
    wechat.push(wechat3);

    var uint = [];
    uint.push(reuint1);
    uint.push(reuint2);
    uint.push(reuint3);

    console.log("uids",uids);
    if(!id ){
        return cb({code:500,info:"参数错误"})
    }
    var obj = {};
    obj.commands = "update taccount set"
        + " sex = '" + sex + "',"
        + " birthday = '" + birthday + "',"
        + " IdCard = '" + IdCard + "',"
        + " phone = '" + phone + "',"
        + " roomId = '" + roomId + "',"
        + " hospitalId = '" + hospitalId + "'"
        + " where id = '" + id + "';";

    obj.commands += "update taccountex set"
        + " allergy = '" + allergy + "',"
        + " history = '" + history + "',"
        + " address = '" + address + "',"
        + " total = '" + total + "',"
        + " level = '" + level + "',"
        + " payInfo = '" + payInfo + "',"
       // + " vacate = '" + vacate + "',"
       // + " call = '" + call + "',"
        + " risk = '" + risk + "',"
        + " food = '" + food + "',"
        + " food = '" + food + "',"
        + " feed = '" + feed + "',"
       // + " festival = '" + festival + "',"
       // + " extraMeal = '" + extraMeal + "',"
       // + " WEpay = '" + WEpay + "',"
        + " electric = '" + electric + "',"
        + " bed = '" + bed + "',"
       // + " consumable = '" + consumable + "',"
        + " standbyMoney = '" + standbyMoney + "',"
        + " unit = '" + unit + "',"
        + " intoRoomTime = '" + intoRoomTime + "',"
        + " outRoomTime = '" + outRoomTime + "',"
        + " extendInfo = '" + extendInfo + "'"
    if(url)
        obj.commands +=  " , url = '" + url + "'"

    obj.commands +=" where id = '" + id + "';";


    for(var i in uids){
        if(uids[i]){
            obj.commands += "update trelation set"
                + " name = '" + names[i] + "',"
                + " phone = '" + phones[i]   + "',"
                + " relation = '" + relations[i] + "',"
                + " wechat = '" + wechat[i] + "',"
                + " uint = '" + uint[i] + "'"
                + " where uid = '" + uids[i] +"';"
        }else {
            if(names[i])
                obj.commands += "insert into trelation(id,name,phone,uint,wechat,relation) values("
                    + "'" + id + "',"
                    + "'" + names[i] + "',"
                    + "'" + phones[i] + "',"
                    + "'" + uint[i] + "',"
                    + "'" + wechat[i] + "',"
                    + "'" + relations[i] + "');"
        }
    }





    console.log("obj",obj);

    db.runSqlInfo(obj, function (info, err) {
        console.log("info is",info)
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:"修改成功"});
        }
    });
}




/**
 * 推送详细信息
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var shoPushDetail = function(parame,req,cb) {
    var  id= parame.id;
    console.log("parame is",parame)
    console.log("id is",id)
    if(!id ){
        return cb({code:500,info:"参数错误"})
    }
    var obj = {};
    obj.commands = "select * from pushdetail where id ='" + id + "';"

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
}

/**
 * 通知信息
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var getNoticeInfo = function(parame,req,cb) {
    var obj = {};
    obj.commands = "select * from setordertime;"

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
}


/**
 * 重阳故事
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var getStoryInfo = function(parame,req,cb) {
    var  startTime= parame.startTime;
    var  endTime= parame.endTime;
    var obj = {};
    obj.commands = "select * from story "
         + "where time >= '" + startTime + "'"
         + "and  time <= '" + endTime + "';" ;

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};
/**
 * 重阳故事id查找
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var showStoryDetail = function(parame,req,cb) {
    var  id= parame.id;
    var obj = {};
    obj.commands = "select * from story "
         + "where id = '" + id + "';";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};

/**
 * 重阳故事id查找
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var showStoryDetail = function(parame,req,cb) {
    var  id= parame.id;
    var obj = {};
    obj.commands = "select * from story "
         + "where id = '" + id + "';";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};
/**
 * 重阳故事添加
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var addStoryInfo = function(parame,req,cb) {
    console.log("parame os",req.body);
    var  title= parame.title;
    var  msg= parame.msg;
    var  time= parame.time;
    var  detailTitle= parame.detailTitle;
    var  detailContent= parame.detailContent;
    var obj = {};
    var httpUrl = "";
    var type = 2;

    console.log("parame os",req.files);
    if(req.files.files){
        var url = '/image/storyImage/' + new  Date().getTime() + ".png";
         httpUrl =  url;
        var originalPath = req.files.files.path;
        var targetPath = path.join(__dirname, '../public'+url);
        fs.createReadStream(originalPath).pipe(fs.createWriteStream(targetPath));
        type = 2;
    }

    obj.commands = "insert into story (title,type,detailTitle,detailContent,time,image,msg) values ("
    + "'" +  title + "'"
    + ",'" +  type + "'"
    + ",'" +  detailTitle + "'"
    + ",'" +  detailContent + "'"
    + ",'" +  time + "'"
    + ",'" +  httpUrl + "'"
    + ",'" +  msg + "');";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};



/**
 * 删除重阳故事
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var delStoryInfo = function(parame,req,cb) {
    var  id= parame.id;
    var obj = {};
    obj.commands = "delete  from story "
        + "where id = '" + id + "';";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};

/**
 * 重阳故事
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var getNewBornInfo = function(parame,req,cb) {
    var  startTime= parame.startTime;
    var  endTime= parame.endTime;
    var obj = {};
    obj.commands = "select * from newborn "
         + "where time >= '" + startTime + "'"
         + "and  time <= '" + endTime + "';" ;

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};
/**
 * 重阳故事id查找
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var showNewBornDetail = function(parame,req,cb) {
    var  id= parame.id;
    var obj = {};
    obj.commands = "select * from  newborn"
         + "where id = '" + id + "';";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};

/**
 * 重阳故事id查找
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var showNewBornDetail = function(parame,req,cb) {
    var  id= parame.id;
    var obj = {};
    obj.commands = "select * from newborn "
         + "where id = '" + id + "';";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};
/**
 * 重阳故事添加
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var addNewBornInfo = function(parame,req,cb) {
    console.log("parame os",parame);
    var  title= parame.title;
    var  msg= parame.msg;
    var  time= parame.time;
    var  detailTitle= parame.detailTitle;
    var  detailContent= parame.detailContent;
    var obj = {};
    var httpUrl = "";
    var type = 2;

    console.log("parame os",req.files);
    if(req.files.files){
        var url = '/image/newbornImage/' + new  Date().getTime() + ".png";
        httpUrl = url;
        var originalPath = req.files.files.path;
        var targetPath = path.join(__dirname, '../public'+url);
        fs.createReadStream(originalPath).pipe(fs.createWriteStream(targetPath));
        type = 2;
    }


    obj.commands = "insert into newborn (title,type,detailTitle,detailContent,time,image,msg) values ("
    + "'" +  title + "'"
    + ",'" +  type + "'"
    + ",'" +  detailTitle + "'"
    + ",'" +  detailContent + "'"
    + ",'" +  time + "'"
    + ",'" +  httpUrl + "'"
    + ",'" +  msg + "');";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};



/**
 * 删除重阳故事
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var delNewBornInfo = function(parame,req,cb) {
    var  id= parame.id;
    var obj = {};
    obj.commands = "delete  from newborn "
        + "where id = '" + id + "';";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};
/**
 * 显示详细内容
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */

var showTaccountDetail = function(parame,req,cb) {
    var  id= parame.id;
    var  flag= parame.flag;
    var obj = {};
    obj.commands = "select * from `" + flag +  "` where uin  = " + id;

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};

/**
 * 添加详细内容
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */

var addDetail = function(parame,req,cb) {
    var  id= parame.id;
    var  flag= parame.type;
    var  info= parame.info;
    var  time= parame.time;
    var obj = {};
    obj.commands = "insert into  `" + flag +  "` (time,info,uin) values( "
        + "'" + time + "',"
        + "'" + info + "',"
        + "'" + id + "')";

    db.runSqlInfo(obj, function (info, err) {
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:info});
        }
    });
};

/**
 * 获取護工名單
 * @param parame
 * @param req
 * @param cb
 */
var getNurseList = function (parame,req,cb) {
    var phone = parame.phone;

    var obj = {};
    obj.commands = "select * from employee where type = 1;";
    if(phone)
        obj.commands = "select * from employee where phone = '" + phone + "' where type = 1;";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"修改失败"});
        else
            cb({code:200,info:info});
    })
};

/**
 * 获取管理员名單
 * @param parame
 * @param req
 * @param cb
 */
var getGovernorList = function (parame,req,cb) {
    var phone = parame.phone;

    var obj = {};
    obj.commands = "select * from employee where type = 2;";
    if(phone)
        obj.commands = "select * from employee where phone = '" + phone + "' where type = 2;";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"修改失败"});
        else
            cb({code:200,info:info});
    })
};

/**
 * 添加员工信息
 * @param parame
 * @param req
 * @param cb
 */
var AddEmployee = function (parame,req,cb) {

    var employeeUin = parame.employeeUin;
    var name = parame.name;
    var sex = parame.sex;
    var phone = parame.phone;
    var birthday = parame.birthday;
    var onDuty = parame.onDuty;
    var type = parame.type;


    var obj = {};
    obj.commands = "insert into  employee (employeeUin,name,sex,phone,birthday,onDuty,type) values ("
        + "'" + employeeUin + "'"
        + ",'" + name + "'"
        + ",'" + sex + "'"
        + ",'" + phone + "'"
        + ",'" + birthday + "'"
        + ",'" + onDuty + "'"
        + ",'" + type + "'"
        + ");";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is:",info);
        if(err)
            return cb({code:500,info:"添加失败"});
        else
            cb({code:200,info:"添加成功"});
    })
};

var empoloyeeInfo = function (req,res,cb) {
    var form = req.query;
    console.log("form is :",typeof  form,req.query);

    var id = form.id;//.toLowerCase();

    var obj = {};
    obj.commands = "select * from employee where id = '" + id + "'";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is",info)
        if (err) {
            res.redirect('/boss/index')
        }
        else {
            res.render('boss/employeeInfo', {
                data:info[0],
            })
        }
    })
}

/**
 * 获取员工详细信息
 * @param req
 * @param res
 * @param cb
 */


var getEmployeeDetail = function (parame,req,cb) {
    console.log("parame is :",parame);
    var id = parame.id;//.toLowerCase();
    var type = parame.type;//.toLowerCase();

    var obj = {};
    obj.commands = "select * from employee_detail where employeeUin = '" + id + "' and type = '" + type + "';";

    db.runSqlInfo(obj,function(info,err){
        console.log("info is",info)
        if (err) {
            cb({code:500,info:'获取数据失败'})
        }
        else {
            cb({code:200,info:info})
        }
    })
}



var AddEmployeeDetail = function (parame,req,cb) {
    console.log("parame is :",parame);
    var employeeUin = parame.employeeUin;//.toLowerCase();
    var startDate = moment(parame.startDate).format("YYYY-MM-DD");//.toLowerCase();
    var endDate = moment(parame.endDate).format("YYYY-MM-DD");//.toLowerCase();
    var title = parame.title;//.toLowerCase();
    var type = parame.type;//.toLowerCase();
    var content = parame.content;//.toLowerCase();

    var obj = {};
    obj.commands = "insert into employee_detail(startDate,endDate,title,content,employeeUin,type) values ("
        + "'" + startDate + "'"
        + ",'" + endDate + "'"
        + ",'" + title + "'"
        + ",'" + content + "'"
        + ",'" + employeeUin + "'"
        + ",'" + type + "'"
        + ")"
    ;

    db.runSqlInfo(obj,function(info,err){
        console.log("info is",info)
        if (err) {
            cb({code:500,info:'获取数据失败'})
        }
        else {
            cb({code:200,info:'添加成功'})
        }
    })
}

var deleteEmployeeDetail = function (parame,req,cb) {
    console.log("parame is :",parame);
    var id = parame.id;//.toLowerCase();


    var obj = {};
    obj.commands = "delete from   employee_detail where id = "
        + "'" + id + "';"
    ;

    db.runSqlInfo(obj,function(info,err){
        console.log("info is",info)
        if (err) {
            cb({code:500,info:'获取数据失败'})
        }
        else {
            cb({code:200,info:'删除成功'})
        }
    })
}


/**
 * 修改员工信息
 * @param parame
 * @param req
 * @param cb
 * @returns {*}
 */
var updateEmployee = function(parame,req,cb) {
    console.log("parame",parame)

    var parame = JSON.parse(parame.obj);
    var  id= parame.id;
    if(!id ){
        return cb({code:500,info:"参数错误"})
    }
    var url = "";

    if(req.files.files){
        var originalPath = req.files.files.path;
        var targetPath = path.join(__dirname, '../public/image/employee/'+ id  +".png");
        fs.createReadStream(originalPath).pipe(fs.createWriteStream(targetPath));
        //var httpUrl = conf.url;
         url = '/image/employee/'+ id +".png";
    }


    var employeeUin = parame.employeeUin;
    var sex = parame.sex;
    var phone = parame.phone;
    var birthday = parame.birthday;
    var IdCard = parame.IdCard;
    var onDuty = parame.onDuty;
    var workYears = parame.workYears;
    var outDuty = parame.outDuty;
    var address = parame.address;
    var koseki = parame.koseki;
    var resume = parame.resume;
    var matePhone = parame.matePhone;
    var childrenPhone = parame.childrenPhone;
    var culture = parame.culture;


    var obj = {};
    obj.commands = "update employee set"
        + " employeeUin = '" + employeeUin + "',"
        + " sex = '" + sex + "',"
        + " birthday = '" + birthday + "',"
        + " IdCard = '" + IdCard + "',"
        + " phone = '" + phone + "',"
        + " onDuty = '" + onDuty + "',"
        + " workYears = '" + workYears + "',"
        + " outDuty = '" + outDuty + "',"
        + " address = '" + address + "',"
        + " koseki = '" + koseki + "',"
        + " resume = '" + resume + "',"
        + " matePhone = '" + matePhone + "',"
        + " childrenPhone = '" + childrenPhone + "',"
        + " culture = '" + culture + "',"
        + " url = '" + url + "'"
        + " where id = '" + id + "';";


    console.log("obj",obj);

    db.runSqlInfo(obj, function (info, err) {
        console.log("info is",info)
        if (err) {
            return cb({code:500,info:"数据库查询错误"});
        }
        else {
            return cb({code:200,info:"修改成功"});
        }
    });
}


exports.updateEmployee =updateEmployee;
exports.deleteEmployeeDetail =deleteEmployeeDetail;
exports.AddEmployeeDetail =AddEmployeeDetail;
exports.getEmployeeDetail =getEmployeeDetail;
exports.empoloyeeInfo =empoloyeeInfo;
exports.getNurseList =getNurseList;
exports.getGovernorList =getGovernorList;
exports.AddEmployee =AddEmployee;

exports.AddTaccountDetail =AddTaccountDetail;
exports.addDetail =addDetail;
exports.getNewBornInfo =getNewBornInfo;
exports.delNewBornInfo =delNewBornInfo;
exports.addNewBornInfo =addNewBornInfo;
exports.showNewBornDetail =showNewBornDetail;

exports.delStoryInfo =delStoryInfo;
exports.addStoryInfo =addStoryInfo;
exports.showStoryDetail =showStoryDetail;
exports.getStoryInfo =getStoryInfo;

exports.shoPushDetail =shoPushDetail;
exports.getAccountList =getAccountList;
exports.updatePhone =updatePhone;
exports.saveAccount =saveAccount;
exports.getBusOrder =getBusOrder;
exports.taccountInfo =taccountInfo;
exports.getTaccountAppList =getTaccountAppList;
exports.updateTaccountApp =updateTaccountApp;
exports.getOrderInfo =getOrderInfo;
exports.changeBusTime =changeBusTime;
exports.delBusTime =delBusTime;
exports.addBussTime =addBussTime;
exports.updateTaccount =updateTaccount;
exports.getPushInfo =getPushInfo;
exports.pushJG =pushJG;
exports.getNoticeInfo =getNoticeInfo;
exports.showTaccountDetail =showTaccountDetail;

